<script lang="ts">
export default {
  name: "typeLabel"
}
</script>


<script setup lang="ts">
import {onMounted, ref, toRefs, onBeforeMount } from "vue";
import {ElForm, ElMessage, ElMessageBox, ElTable} from "element-plus";
import {getCurrentInstance, reactive} from "vue";
import {ServiceTypeLabel, ServiceTypeLabelQuery} from "@/api/service-type-label/types";
import {Delete, Plus, Refresh, Search} from "@element-plus/icons-vue";
import {useRoute} from "vue-router";
import {
  getServiceLabelForm,
  listServiceLabelPages,
  serviceLabelAdd,
  serviceLabelDelete,
  serviceLabelUpdate
} from "@/api/service-type-label";
import {listServiceTypeOptions} from "@/api/service-type";

const queryFormRef = ref(ElForm); // 查询表单
const dataFormRef = ref(ElForm); // 服务类型表单
const dataTableRef = ref(ElTable);

const {proxy}: any = getCurrentInstance();

const emit = defineEmits(['serviceTypeLabelClick']);


const route = useRoute()

const state = reactive({
// 遮罩层
  loading: false,
// 选中数组
  ids: [] as number[],
// 总条数
  total: 0,
  formData: {} as ServiceTypeLabel,
  serviceTypeLabelList: [] as ServiceTypeLabel[],
  // 服务类型下拉选项
  serviceTypeOptions: [] as OptionType[],
  enableOptions: [] as OptionType[],
  dialog: {
    visible: false,
  } as DialogType,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  } as ServiceTypeLabelQuery,
  rules: {
    typeId: [{ required: true, message: '请选择服务类型', trigger: 'blur' }],
    name: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
  },
});

const {
  ids,
  loading,
  formData,
  queryParams,
  enableOptions,
  serviceTypeOptions,
  serviceTypeLabelList,
  total,
  dialog,
  rules,
} = toRefs(state);
/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  listServiceLabelPages(state.queryParams).then(({ data }) => {
    state.serviceTypeLabelList = data.list;
    state.total = data.total;
    state.loading = false;
  });
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

function handleSelectionChange(selection: any) {
  state.ids = selection.map((item: any) => item.id);
}

function handleRowClick(row: any) {
  emit('serviceTypeLabelClick', row);
}


async function getServiceTypeOptions() {
  listServiceTypeOptions().then((response) => {
    state.serviceTypeOptions = response.data;
  });
}

function handleAdd() {
  dialog.value = {
    title: '新增类型标签',
    visible: true,
  };
  formData.value.typeId = queryParams.value.typeId as string
}

/**
 * 修改
 */
async function handleUpdate(row: any) {
  const serviceLabelId = row.id
  state.dialog = {
    title: '修改服务类型',
    visible: true,
  };
  getServiceLabelForm(serviceLabelId).then((response: any) => {
    let data = response.data
    data.isCertficate = data.isCertficate === 1
    state.formData = response.data;
  });
}

function handleSubmit() {
  loading.value = true;
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      let formData =  state.formData
      formData.isCertficate = formData.isCertficate ? 1 : 0
      if (formData.id) {
        serviceLabelUpdate(formData).then(() => {
          ElMessage.success('修改成功');
          closeDialog();
          handleQuery();
        });
      } else {
        serviceLabelAdd(formData).then(() => {
          ElMessage.success('新增成功');
          closeDialog();
          handleQuery();
        });
      }
    }
  });
}

/**
 * 取消
 */
function closeDialog() {
  dialog.value.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
}

/**
 *  删除
 */
function handleDelete(row: any) {
  const id = row.id
  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    serviceLabelDelete(id).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  }).catch(() => ElMessage.info('已取消删除'));
}

function listEnableOptions() {
  proxy.$listDictItemsByTypeCode('enable_status').then((response: any) => {
    state.enableOptions = response?.data;
  });
}

onMounted(() => {
  console.log("route", route.query)
  console.log(history)
  queryParams.value.typeId = route.query.typeId as string;
  // 初始化服务类型下拉选项
  getServiceTypeOptions();
  listEnableOptions();
  // 初始化服务类型列表数据
  handleQuery();
});

</script>


<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="服务类型">
          <el-select
            v-model="queryParams.typeId"
            placeholder="请选择服务类型"
            clearable
            style="width: 200px"
          >

            <el-option
              v-for="item in serviceTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="name" label="标签名称">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入标签名称进行搜索"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item prop="code" label="标签编码">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入标签编码进行搜索"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-select
            v-model="queryParams.state"
            placeholder="全部"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="item in enableOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleQuery"
          >搜索
          </el-button
          >
          <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>


    <el-card>
      <template #header>
        <el-button type="success" :icon="Plus" @click="handleAdd">新增</el-button>
<!--        <el-button-->
<!--          type="danger"-->
<!--          :icon="Delete"-->
<!--          :disabled="ids.length === 0"-->
<!--          @click="handleDelete"-->
<!--        >删除</el-button>-->
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="serviceTypeLabelList"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        highlight-current-row
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="服务类型" align="center" prop="typeId" width="150" />
        <el-table-column label="服务类型标签名称" align="center" prop="name" width="150" />
        <el-table-column label="服务类型编码" align="center" prop="code" width="160" />
        <el-table-column label="是否关联证件"  align="center" prop="status" width="150">
          <template #default="scope">
            {{scope.row.isCertficate==0?'否':'是'}}
          </template>
        </el-table-column>
        <el-table-column label="状态"  align="center" prop="status" width="150">
          <template #default="scope">
            <el-tag v-if="scope.row.state === 1" type="success">启用</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime"  label="创建时间" align="center" width="160" />
        <el-table-column prop="updateTime" label="修改时间" align="center" width="160" />

        <el-table-column fixed="right" label="操作" align="center" width="160">
          <template #default="scope">
            <el-button
              type="success"
              link
              @click.stop="handleUpdate(scope.row)"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              link
              @click.stop="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- pagination -->
      <pagination
        v-if="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>


    <!-- dialog -->
    <el-dialog
      :title="dialog.title"
      v-model="dialog.visible"
      width="600px"
      @close="closeDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="服务类型" prop="typeId">
          <el-select
            v-model="formData.typeId"
            placeholder="请选择服务类型"
            clearable
            style="width: 200px"
          >

            <el-option
              v-for="item in serviceTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入服务标签名称" />
        </el-form-item>
        <el-form-item label="是否需要证件">
          <el-switch v-model="formData.isCertficate" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="formData.state">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
